// vite.config.ts
import react from '@vitejs/plugin-react';
import { proxy } from './build/proxy.ts';
import { UserConfig, ConfigEnv, loadEnv } from 'vite';
import { resolve } from 'path';
import { viteMockServe } from 'vite-plugin-mock';
const root: string = process.cwd();

export default ({ command, mode }: ConfigEnv): UserConfig => {
  // 获取当前环境的配置
  // 获取当前环境的配置
  const {
    VITE_PORT,
    VITE_PUBLIC_PATH,
    VITE_OUT_DIR,
    VITE_ASSET_DIR,
    VITE_BASEURL,
  }: any = loadEnv(mode, root);
  const isDev = process.env.NODE_ENV === 'development';
  loadEnv(mode, root);
  return {
    plugins: [
      react(),
      // mock配置
      viteMockServe({
        // default
        mockPath: './src/mock',
        enable: true,
      }),
    ],
    // css相关
    css: {
      // 引入全局scss |less 指定传递给 CSS 预处理器的选项。
      preprocessorOptions: {
        less: {
          additionalData:
            '@import "./src/styles/variable.less";@import "@/styles/mixin.less";',
        },
      },
    },
    // 服务相关
    server: {
      https: false,
      port: VITE_PORT,
      proxy: proxy(VITE_BASEURL),
    },
    resolve: {
      alias: {
        '@': resolve(__dirname, './src'),
      },
    },
  };
};
